package com.yyi.springcloud.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 * 隐藏关键信息的工具类
 * 
 */
public class HideDataUtil {

	/**
	 * 隐藏手机号(前三后四)
	 * @param cellphoneNo
	 * @return
	 */
	public static String maskCellphone(String cellphoneNo) {
		if (cellphoneNo == null || cellphoneNo.trim().length() != 11) {
			return cellphoneNo;
		}
		return new StringBuilder().append(cellphoneNo.substring(0, 3)).append("****").append(cellphoneNo.substring(cellphoneNo.length() - 4)).toString();
	}



	/**
	 * 隐藏身份证号码(前四后四)
	 * @param idCardNo
	 * @return
	 */
	public static String maskIDCardNo(String idCardNo) {
		if (idCardNo == null || idCardNo.trim().length() <= 8) {
			return idCardNo;
		}
		idCardNo = idCardNo.trim();
		int length = idCardNo.length();
		String firstFourNo = idCardNo.substring(0, 4);
		String lastFourNo = idCardNo.substring(length - 4);
		StringBuffer mask = new StringBuffer("");
		for (int i = 0; i < length - 8; i++) {
			mask.append("*");
		}
		return firstFourNo + mask.toString() + lastFourNo;
	}

	/**
	 * 隐藏银行卡号码(前六后四 )
	 * @param cardNo
	 * @return
	 */
	public static String maskBankCardNo(String cardNo) {
		if (StringUtils.isBlank(cardNo)) {
			return cardNo;
		}
		if (cardNo.length() > 10) {
			//前六后四
			StringBuilder stringBuilder = new StringBuilder();
			return stringBuilder.append(cardNo.substring(0, 6)).append("****")
					.append(cardNo.substring(cardNo.length() - 4)).toString();
		} else {
			return cardNo;
		}
	}

	public static void main(String[] args) {
		System.out.println("身份证的隐藏后信息为"+maskIDCardNo("370682199610236711"));
		System.out.println("手机号的隐藏后信息为"+maskCellphone("15610542351"));
		System.out.println("银行卡的隐藏后信息为"+maskBankCardNo("6214835654214475"));
	}
}
